package b1.scenes.fight
{
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.Joints.*;
	
	import b1.conifg.*;
	import b1.scenes.*;
	
	import com.greensock.TweenLite;
	
	public class BoneCluster
	{
		public var boneIndex:int;
		public var number:int;
		public var bones:Array;
		public var joints:Array;
		
		public function BoneCluster(px:Number, py:Number, boneIndex:int, number:int)
		{
			this.boneIndex = boneIndex;
			this.number = number;
			
			bones = new Array();
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			var prevBone:Bone = new Bone(boneIndex);
			var boneConfig:ObjectConfig = Config.objectConfigs[boneIndex];
			prevBone.createB2Body(fight.world, px - 15, py, prevBone.width, prevBone.height, boneConfig.density, boneConfig.restitution);
			bones.push(prevBone);
//			fight.addChild(prevBone);
//			var jointDef:b2RevoluteJointDef = new b2RevoluteJointDef();
			var jointDef:b2WeldJointDef = new b2WeldJointDef();
			joints = new Array();
			for (var i:int = 0; i < number - 1; i++)
			{
				var bone:Bone = new Bone(boneIndex);
				bone.createB2Body(fight.world, px + 15, py, bone.width, bone.height, boneConfig.density, boneConfig.restitution);
				bones.push(bone);
				fight.addChild(bone);
				jointDef.Initialize(prevBone.objectB2Body, bone.objectB2Body, prevBone.objectB2Body.GetWorldCenter());
				var bj:b2Joint = fight.world.CreateJoint(jointDef);
				joints.push(bj);
			}
		}
		
		public function split():void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			for (var i:int = 0; i < joints.length; i++)
				fight.world.DestroyJoint(joints[i]);
//			for (i = 0; i < bones.length; i++)
//			{
//				var bone:Bone = bones[i];
//				var velocity:b2Vec2 = bone.objectB2Body.GetLinearVelocity();
//				var mass:Number = bone.objectB2Body.GetMass();
//				TweenLite.delayedCall(0.2, bone.applyForce, [b2Math.RandomRange(10, 30) * velocity.x * mass, b2Math.RandomRange(10, 30) * velocity.y * mass]);
//			}
		}

	}
}